package com.cyy.user.controller;

import com.cyy.user.pojo.User;
import com.cyy.user.service.UserService;
import entity.Result;
import entity.StatusCode;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;
import util.JwtUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * user控制器层
 * @author Administrator
 *
 */
@RestController
@CrossOrigin
@RequestMapping("/user")
public class UserController {

	@Autowired
	private UserService userService;

	@Autowired
	private JwtUtils jwtUtils;

	@Autowired
	private HttpServletRequest request;

	/**
	 * 发送短信
	 * @param mobile
	 * @return
	 */
	@RequestMapping(value="/sendSms/{mobile}",method = RequestMethod.GET)
	public Result sendSms(@PathVariable String mobile){
		userService.sendSms(mobile);
		return new Result(true, StatusCode.OK,"发送成功");
	}

	/**
	 * 注册用户
	 * @param user
	 * @param code
	 * @return
	 */
	@RequestMapping(value="/register/{code}",method = RequestMethod.POST)
	public Result register(@RequestBody User user, @PathVariable String code){
		 userService.add(user,code);
		 return new Result(true,StatusCode.OK,"注册成功");
	}

	/**
	 * 保存用户
	 */
	@RequestMapping(method = RequestMethod.POST)
	public Result add(@RequestBody User user){
		userService.add(user);
		return new Result(true,StatusCode.OK,"添加成功");
	}

	@RequestMapping(value= "/login",method = RequestMethod.POST)
	public Result login(@RequestBody Map<String,String> map){
		User user = userService.findByMoblieAndPassword(map.get("mobile"),map.get("password"));
		if(user != null){
			Map<String,String> infoMap = new HashMap<>();
			String token = jwtUtils.createJwt(user.getId(),"user",user.getNickname());
			infoMap.put("token",token);
			infoMap.put("nickname",user.getNickname());//昵称
			infoMap.put("avatar",user.getAvatar());//头像
			return new Result(true,StatusCode.OK,"登录成功",infoMap);
		}else{
			return new Result(false,StatusCode.LOGINERROR,"手机号或密码错误");
		}
	}

	/**
	 * 查询所有用户
	 * @return
	 */

	@RequestMapping(method= RequestMethod.GET)
	public Result findAll(){
		return new Result(true,StatusCode.OK,"查询成功",userService.findAll());
	}

	/**
	 * 按Authorization头信息约定的规定鉴权 Bear + 空格 + token admin角色才拥有删除权限
	 * 删除
	 * @param id
	 */

	@RequestMapping(value="/{id}",method= RequestMethod.DELETE)
	public Result delete(@PathVariable String id){
		Claims claims = (Claims) request.getAttribute("admin_claims");
		if(claims == null){
			return new Result(false,StatusCode.ACCESSERROR,"权限不足");
		}
		userService.deleteById(id);
		return new Result(true,StatusCode.OK,"删除成功");
	}

	/**
	 * 增加粉丝数
	 */
	@RequestMapping(value = "/incFans/{userid}/{fan}",method = RequestMethod.GET)
	public void incFansCount(@PathVariable String userid,@PathVariable int fan){
		userService.incFanscount(userid,fan);
	}

	/**
	 * 增加粉丝关注数
	 */
	@RequestMapping(value = "/incFollows/{userid}/{fan}",method = RequestMethod.GET)
	public void incFollowCount(@PathVariable String userid,@PathVariable int fan){
		userService.incFollowcount(userid,fan);
	}
}
